package com.koron.operation.dispatch.controller;

import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.operation.dispatch.bean.vo.AffectedLineVo;
import com.koron.operation.dispatch.bean.vo.AffectedTranVo;
import com.koron.operation.dispatch.bean.vo.AffectedUnitVo;
import com.koron.operation.dispatch.feign.DeviceFeign;
import com.koron.operation.dispatch.service.EquipmentRunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/equipment/Analysis")
@RestController
@Api(tags = "调度管理-状态汇总")
public class EquipmentAnalysisController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private EquipmentRunService equipmentrunservice;

    @Autowired
    DeviceFeign deviceservice;


    @ApiOperation(value = "线路状态汇总-列表")
    @GetMapping("/lineAnalysis")
    public String lineAnalysis() {
//        List<AffectedLineVo> result = new EquipmentRunServiceDeImp().lineAnalysis();
        List<AffectedLineVo> result = ADOConnection.runTask(getCurrDs(), equipmentrunservice, "lineAnalysis", List.class);
        return toJson(Response.ok(result));
    }

    @ApiOperation(value = "机组状态汇总-列表")
    @GetMapping("/unitAnalysis")
    public String unitAnalysis() {
        List<AffectedUnitVo> result = ADOConnection.runTask(getCurrDs(), equipmentrunservice, "unitAnalysis", List.class);
        return toJson(Response.ok(result));
    }

    @ApiOperation(value = "主变状态汇总-列表")
    @GetMapping("/tranAnalysis")
    public String tranAnalysis() {
        List<AffectedTranVo> result = ADOConnection.runTask(getCurrDs(), equipmentrunservice, "tranAnalysis", List.class);
        return toJson(Response.ok(result));
    }

    private String toJson(Object obj){
        return new GsonBuilder()
                .setLongSerializationPolicy(LongSerializationPolicy.STRING)
                .setDateFormat("yyyy-MM-dd HH:mm:ss SSS")
                .create().toJson(obj);
    }


}
